#!/bin/bash

function rootfs_install()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"
	local TMPROOTFS=tmprootfs
	mkdir -p ${TMPROOTFS}
	mkdir -p ${TMPROOTFS}/etc/
	mkdir -p ${TMPROOTFS}/opt/rk/
	echo -e '#!/bin/bash\n' >  ${TMPROOTFS}/etc/rc.local
	cat ./rc.local.camera \
							>> ${TMPROOTFS}/etc/rc.local
	sed "s/_AUD_DEV_PLACEHOLDER_/APE/g" ./rc.local.orin.audio \
							>> ${TMPROOTFS}/etc/rc.local
	sed "s/_AUD_DEV_PLACEHOLDER_/APE/g" ./jetson_aplay.sh \
					> ${TMPROOTFS}/opt/rk/jetson_aplay.sh
	sed "s/_AUD_DEV_PLACEHOLDER_/APE/g" ./jetson_arecord.sh \
					> ${TMPROOTFS}/opt/rk/jetson_arecord.sh
	cp ./jetson_camera.sh ${TMPROOTFS}/opt/rk/jetson_camera.sh
	cp ./jetson_pwm.sh ${TMPROOTFS}/opt/rk/
	cp ./jetson_can.sh ${TMPROOTFS}/opt/rk/
	cp ./aud_picker.sh.orin ${TMPROOTFS}/opt/rk/aud_picker.sh
	cp ./song-youth.wav ${TMPROOTFS}/opt/rk

	# RK Sensors Demo
	local RK_SENSORS_SRC_URL="gitea@192.168.1.55:pan.kang/rk_sensors_RKNS0AN0.git"
	local RK_SENSORS_SRC_DIR="${HOME}/nvidia/source_code/rk_sensors_RKNS0AN0"
	if [[ ! -d ${RK_SENSORS_SRC_DIR} ]]; then
		git clone ${RK_SENSORS_SRC_URL} ${RK_SENSORS_SRC_DIR}
	fi
	if [[ ! -f ${RK_SENSORS_SRC_DIR}/rk_sensors ]]; then
		make -C ${RK_SENSORS_SRC_DIR} \
			CROSS_COMPILE=${L4T_TOOLCHAIN}
	fi
	mkdir -p ${TMPROOTFS}/opt/rk/rk_sensors
	cp ${RK_SENSORS_SRC_DIR}/rk_sensors ${TMPROOTFS}/opt/rk/rk_sensors
	cp ${RK_SENSORS_SRC_DIR}/README.md ${TMPROOTFS}/opt/rk/rk_sensors

	# Quectel
	local QUECTEL_SRC_URL="gitea@192.168.1.55:pan.kang/quectel_service.git"
	local QUECTEL_SRC_DIR="${HOME}/nvidia/source_code/quectel_service"
	if [[ ! -d ${QUECTEL_SRC_DIR} ]]; then
		git clone ${QUECTEL_SRC_URL} ${QUECTEL_SRC_DIR} --recursive
	fi
	sudo_cmd "cp ${QUECTEL_SRC_DIR}/quectel.service ${L4T_TOP}/rootfs/lib/systemd/system/"

	local QUECTEL_SRC_CM_DIR="${QUECTEL_SRC_DIR}/Quectel_QConnectManager_Linux_V1.6.4"
	if [[ ! -f ${QUECTEL_SRC_CM_DIR}/quectel-CM ]]; then
		make -C ${QUECTEL_SRC_CM_DIR} \
			CROSS_COMPILE=${L4T_TOOLCHAIN}
	fi

	local TMPROOTFS_OPT_RK_QUECTEL=${TMPROOTFS}/opt/rk/quectel
	mkdir -p ${TMPROOTFS_OPT_RK_QUECTEL}
	cp ${QUECTEL_SRC_CM_DIR}/quectel-CM \
		${QUECTEL_SRC_DIR}/README.md \
		${QUECTEL_SRC_DIR}/Quectel_RG200U*.pdf \
		${QUECTEL_SRC_DIR}/Quectel_EC20*.pdf \
		${TMPROOTFS_OPT_RK_QUECTEL}

	local TMPROOTFS_PATH=$(abs_path "${TMPROOTFS}")
	local TMPROOTFS_PATH_LEVEL=$(abs_path_level "${TMPROOTFS_PATH}")
	traverse ${TMPROOTFS_PATH} traverse_filter_none 0 traverse_exec_install ${TMPROOTFS_PATH_LEVEL} ${L4T_TOP}/rootfs
	rm -rf ${TMPROOTFS}
}

function rootfs_uninstall()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	sudo_cmd "rm -rf	${L4T_TOP}/rootfs/etc/rc.local \
						${L4T_TOP}/rootfs/opt/rk/jetson_aplay.sh \
						${L4T_TOP}/rootfs/opt/rk/jetson_arecord.sh \
						${L4T_TOP}/rootfs/opt/rk/jetson_camera.sh \
						${L4T_TOP}/rootfs/opt/rk/jetson_pwm.sh \
						${L4T_TOP}/rootfs/opt/rk/jetson_can.sh \
						${L4T_TOP}/rootfs/opt/rk/aud_picker.sh \
						${L4T_TOP}/rootfs/opt/rk/song-youth.wav \
						${L4T_TOP}/rootfs/opt/rk/rk_sensors \
						${L4T_TOP}/rootfs/opt/rk/quectel \
						${L4T_TOP}/rootfs/lib/systemd/system/quectel.service \
						${L4T_TOP}/rootfs/opt/rk"
}

#############################################################

if [[ $# -le 0 ]]; then
	exit 0
fi

case $1 in
	"install" )
		rootfs_install
		;;
	"uninstall" )
		rootfs_uninstall
		;;
esac
